import { equals } from "../equals";
import { isFunction } from "../isFunction";
import { isObject } from "../isObject";

/**
 * @description 匹配 Request Key 是否存在
 * @author lihh
 * @param request 请求headers
 * @param key 匹配的key
 */
export function isMatchHeaderKey(request: Request, key: string): boolean {
  if (
    !isObject(request) ||
    !isObject(request.headers) ||
    !(request.headers instanceof Headers) ||
    !isFunction(request.headers.get) ||
    !isFunction(request.headers.has)
  ) {
    return false;
  }

  return request.headers.has(key);
}

/**
 * @description 匹配 Request Key 是否存在
 * @author lihh
 * @param request 请求headers
 * @param key 匹配的key
 * @param value 匹配的 value
 */
export function isMatchHeaderValue(
  request: Request,
  key: string,
  value: string
): boolean {
  if (!isMatchHeaderKey(request, key)) {
    return false;
  }

  return equals(request.headers.get(key), value);
}
